<?php

namespace RTSCraft\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use RTSCraft\MainBundle\Custom\Result;
use RTSCraft\MainBundle\Entity\Article;
use RTSCraft\MainBundle\Entity\Version;

class ResearchController extends Controller
{
    /**
     * @Route("/", name="results")
     * @Template()
     */
    public function indexAction()
    {
      // index page
      return $this->render('RTSCraftMainBundle:Default:results_2.html.twig');
    }

    /**
     * @Route("/", name="results2")
     * @Template()
     */
    public function resultsAction()
    {
      return $this->render('RTSCraftMainBundle:Default:results_2.html.twig');
    }

    public function searchAction()
    {
      $template = '';
      $request = $this->get('request');

      if ($request->getMethod() == 'POST')
	{
	  $template = $request->request->get('search');
	  $template = str_replace('*', '%', $template);
	  $template = str_replace('?', '_', $template);
	}

      $rep = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Article');

      $results = $rep->getLastVersions($template);

      /*	-- DEBUG --
      $response = '<pre>';
      $response .= 'Results for ' . $template . ': <br />';
      $response .= print_r($results, true);
      $response .= '</pre>';
      return new Response($response); */

      return $this->render('RTSCraftMainBundle:Default:results_2.html.twig', array('results' => $results));
    }
}








